﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using Core.Model;
using Core.Model.Column;
using Core.Shared.Monads;

namespace UI.Resources.Controls {
	public partial class ColumnsSelector : IDisposable {
		public ColumnsSelector() {
			InitializeComponent();
		}

		~ColumnsSelector() {
			Dispose(false);
		}

		#region Public fields

		public ObservableCollection<IExaminationModel> SourceItems {
			get { return (ObservableCollection<IExaminationModel>) GetValue(SourceItemsProperty); }
			set { SetValue(SourceItemsProperty, value); }
		}

		public ObservableCollection<ExaminationColumn> ResultItems {
			get { return (ObservableCollection<ExaminationColumn>) GetValue(ResultItemsProperty); }
			set { SetValue(ResultItemsProperty, value); }
		}

		#endregion

		#region Dependency properties

		public static readonly DependencyProperty SourceItemsProperty = DependencyProperty.Register(
			"SourceItems", typeof (ObservableCollection<IExaminationModel>), typeof (ColumnsSelector),
			new FrameworkPropertyMetadata(new ObservableCollection<IExaminationModel>()));

		public static readonly DependencyProperty ResultItemsProperty = DependencyProperty.Register(
			"ResultItems", typeof (ObservableCollection<ExaminationColumn>), typeof (ColumnsSelector),
			new FrameworkPropertyMetadata(new ObservableCollection<ExaminationColumn>()));

		#endregion

		#region Private methods

		private void Add(ExaminationColumn column) {
			if (column.Type == ColumnType.Identifier && ResultItems.Any(c => c.Type == ColumnType.Identifier)) {
				return;
			}

			ResultItems.Add(column);
		}

		#endregion

		#region Event handlers

		private void ToResultButtonClick(object sender, RoutedEventArgs e) {
			ExaminationColumn column;
			if (NewTextBox.Text != String.Empty) {
				column = new ExaminationColumn(NewTextBox.Text, ColumnType.Regular);
				NewTextBox.Text = String.Empty;
			}
			else {
				column = SourceListBox.SelectedItem as ExaminationColumn;
			}

			column.Do(Add);
		}

		private void ToSourceButtonClick(object sender, RoutedEventArgs e) {
			ResultListBox.SelectedItem.Do(i => ResultItems.Remove((ExaminationColumn) i));
		}

		private void TreeViewItemMouseDoubleClick(object sender, MouseButtonEventArgs e) {
			var item = sender as TreeViewItem;
			var column = SourceListBox.SelectedItem as ExaminationColumn;

			if (item != null && item.IsSelected) {
				column.Do(Add);
			}
		}

		private void NewTextBoxKeyDown(object sender, KeyEventArgs e) {
			if (e.Key == Key.Enter) {
				ResultItems.Add(new ExaminationColumn(NewTextBox.Text, ColumnType.Regular));
				NewTextBox.Text = String.Empty;
			}
		}

		#endregion

		#region IDisposable implementation

		public void Dispose() {
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing) {
			if (disposing) {
				ResultItems.Clear();
			}
		}

		#endregion
	}
}